// 重写数组原型方法
let oldArrayProtoMethods = Array.prototype;

export let ArrayMethods = Object.create(oldArrayProtoMethods);

// 劫持的方法
let methods = ['push', 'pop', 'unshift', 'shift', 'splice'];


methods.forEach(item => {
  ArrayMethods[item] = function (...args) {
    // 调用原来的数组方法
    const result = oldArrayProtoMethods[item].apply(this, args);

    let inserted;
    switch (item) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.splice(2);
        break;
    }
    let ob = this.__ob__;
    // 对添加的对象进行劫持
    if (inserted) {
      ob.observeArray(inserted);
    }
    ob.dep.notify();
    return result;
  }
})

